package com.susallwave.srcb.serviceimpl.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.susallwave.srcb.entity.sys.OperationLog;
import com.susallwave.srcb.mapper.sys.OperationLogMapper;
import com.susallwave.srcb.response.Result;
import com.susallwave.srcb.service.sys.OperationLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统操作日志 服务实现类
 * </p>
 *
 * @author MyBatisPlusGenerator
 * @since 2025-07-22
 */
@Service
public class OperationLogServiceImpl extends ServiceImpl<OperationLogMapper, OperationLog> implements OperationLogService {
    @Override
    public Result<IPage<OperationLog>> listLogs(long pageNum, long pageSize, String startTime, String endTime,
                                                OperationLog queryParams) {
        // 创建分页对象
        Page<OperationLog> page = new Page<>(pageNum, pageSize);

        // 构建查询条件
        LambdaQueryWrapper<OperationLog> queryWrapper = new LambdaQueryWrapper<>();

        // 动态添加查询条件（排除 ID）
        if (queryParams != null) {
            // 银行用户ID
            if (StringUtils.isNotBlank(queryParams.getUserAcct())) {
                queryWrapper.eq(OperationLog::getUserAcct, queryParams.getUserAcct());
            }

            // IP地址（支持模糊查询）
            if (StringUtils.isNotBlank(queryParams.getIpAddress())) {
                queryWrapper.like(OperationLog::getIpAddress, queryParams.getIpAddress());
            }

            // 请求URL（支持模糊查询）
            if (StringUtils.isNotBlank(queryParams.getRequestUrl())) {
                queryWrapper.like(OperationLog::getRequestUrl, queryParams.getRequestUrl());
            }

            // 请求方法
            if (StringUtils.isNotBlank(queryParams.getRequestMethod())) {
                queryWrapper.eq(OperationLog::getRequestMethod, queryParams.getRequestMethod());
            }

            // 调用的类方法（支持模糊查询）
            if (StringUtils.isNotBlank(queryParams.getClassMethod())) {
                queryWrapper.like(OperationLog::getClassMethod, queryParams.getClassMethod());
            }

            // 操作状态
            if (queryParams.getStatus() != null) {
                queryWrapper.eq(OperationLog::getStatus, queryParams.getStatus());
            }

            // 耗时范围查询
            if (queryParams.getTimeCost() != null) {
                queryWrapper.ge(OperationLog::getTimeCost, queryParams.getTimeCost());
            }

            // 创建时间范围查询
            if (queryParams.getCreateTime() != null) {
                // mp自动处理string类型时间.
                queryWrapper.between(OperationLog::getCreateTime, startTime, endTime);
            }
        }
        // 默认按创建时间倒序
        queryWrapper.orderByDesc(OperationLog::getCreateTime);
        return Result.success(this.page(page, queryWrapper));
    }
}